$(function() {
	FastClick.attach(document.body);
	setFullScreen(".supervise-comment-content", 20)
});

var detailCommentVue = new Vue({
	el: "body",
	data: {
		isJINGBAN: false,
		isDUBAN: false,
		userid: "",
		isLeader: false, //是否是馆长
		leaderList: ""
	},
	created: function() {
		_this = this;
		_this.userid = getUrlItem("userid");
		_this.userName = $.cookie("BHCJ_wechat_wechatname");
		_this.taskId = getUrlItem("TASKID");
		console.log(_this.taskId)

		$.getJSON("http://47.92.116.158:8099/BhGgjAPI/Supervise.asmx/getHeaderUsers?callback=?", function(jsondata) {
			jsondata.forEach(function(item) {
				_this.leaderList += item.CODE + "|"
				if(item.CODE == _this.userid) {
					_this.isLeader = true
				} else {

				}
			})
			_this.leaderList = _this.leaderList.substring(0, _this.leaderList.length - 1);
		});
	},
	methods: {
		saveComment: function() {
			var time = new Date();
			var timeString = formatDateTime(time)
			var content = $(".supervise-comment-input").html().replace(/&nbsp;/g, " ").trim();

			if(content.length > 0) {
				$.ajax({
					type: "POST",
					crossDomain: true,
					url: "http://47.92.116.158:8099/BhGgjAPI/Supervise.asmx/setComment?callback=?",
					data: {
						"taskcode": _this.taskId,
						"dubanman": _this.userName,
						"dubandate": timeString,
						"dubanopi": content,
						"dubanremark": "xx",
						"duuserid": _this.userid
					},
					dataType: "json",
					success: function(jsondata) {
						console.log(jsondata)
						var taskName = jsondata.taskname

						if(jsondata.taskManIdList != "") {
							var taskList = jsondata.taskManIdList.split(",")
							for(var i = 0; i < taskList.length; i++) {
								if(taskList[i] == _this.userid) {
									_this.isJINGBAN = true
								}
							}
						}

						//对经办人信息进行格式化
						var useridArry2 = jsondata.taskManIdList;
						var set_loastr2 = "";
						var set_loc2 = "";
						if(useridArry2.indexOf(",")) {
							$.each(useridArry2.split(','), function(index, item) {
								item = item.split('=');
								var name2 = item[0];
								set_loastr2 = name2 + "|";
								set_loc2 += set_loastr2;
							});
							set_loc2 = set_loc2.substring(0, set_loc2.length - 1);
							set_loc2 = set_loc2;
						} else {
							set_loc2 = useridArry2;
						}

						if(jsondata.dubanManIdList != null) {
							var duBanList = jsondata.dubanManIdList.split(",")
							for(var i = 0; i < duBanList.length; i++) {
								if(duBanList[i] == _this.userid) {
									_this.isDUBAN = true
								}
							}
						}

						//对督办人信息进行格式化
						var useridArry1 = jsondata.dubanManIdList;
						var set_loastr1 = "";
						var set_loc1 = "";
						if(useridArry1.indexOf(",")) {
							$.each(useridArry1.split(','), function(index, item) {
								item = item.split('=');
								var name1 = item[0];
								set_loastr1 = name1 + "|";
								set_loc1 += set_loastr1;
							});
							set_loc1 = set_loc1.substring(0, set_loc1.length - 1);
							set_loc1 = set_loc1;
						} else {
							set_loc1 = useridArry1;
						}

						alert(set_loc1) //督办人列表
						alert(set_loc2) //经办人列表

						//如果是经办人发表评论，给督办人发消息
						if(_this.isJINGBAN == true) {
							if(set_loc1 != "") {
								//如果督办人未填写则该条消息不推送
								sendDUBANMsg(set_loc1, _this.taskId, taskName, _this.userName)
							}
						}

						//如果是督办人发表评论，给经办人发消息
						if(_this.isDUBAN == true) {
							if(set_loc2 != "") {
								sendJINGBANMsg(set_loc2, _this.taskId, taskName, _this.userName)
								//如果经办人未填写则该条消息不推送
							}
						}

						//如果是馆长，给全部人发消息
						if(_this.isLeader == true) {
							if(set_loc1 != "") {
								//如果督办人未填写则该条消息不推送
								sendDUBANMsg(set_loc1, _this.taskId, taskName, _this.userName)
							}
							if(set_loc2 != "") {
								sendJINGBANMsg(set_loc2, _this.taskId, taskName, _this.userName)
								//如果经办人未填写则该条消息不推送
							}
						}
						//馆长存在消息无需判断一定推送
						sendLeaderMsg(_this.leaderList, _this.taskId, _this.userid, taskName, _this.userName)
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						alert(JSON.stringify(XMLHttpRequest));
						alert(textStatus);
						alert(errorThrown);
					}
				});

			} else {

			}
		},
		clearAll: function() {
			$(".supervise-comment-input").html("")
		},
		back: function() {
			window.history.go(-1); //返回上一页
		}
	}
})
/**
 * 
 * @param {Object} Name   需要设置充满屏幕的对象
 * @param {Object} Height 预留空间
 */
function setFullScreen(Name, Height) {
	$(document).ready(function() { //DOM加载后就执行
		var WindowHeight = $(window).height();
		$(Name).css('height', WindowHeight - Height);
	});
	$(window).resize(function() { //改变窗体大小时     *resize()变化或者改变事件
		var WindowHeight = $(window).height();
		$(Name).css('height', WindowHeight - Height);
	});
	$(window).scroll(function() { //浏览器有滚动条时的操作   scroll()滚动事件
		var WindowHeight = $(window).height();
		$(Name).css('height', WindowHeight - Height);
	});
}

//截取url传参
function getUrlItem(name) {
	var url = location.search
	start = url.indexOf(name + "=")
	if(start != -1) {
		start = start + name.length + 1
		end = url.indexOf("&", start)
		if(end == -1) {
			end = url.length
			return unescape(url.substring(start, end))
		}
		return unescape(url.substring(start, end))

	}
	return '';
}

//对时间对象进行格式化处理
var formatDateTime = function(date) {
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	m = m < 10 ? ('0' + m) : m;
	var d = date.getDate();
	d = d < 10 ? ('0' + d) : d;
	var h = date.getHours();
	var minute = date.getMinutes();
	minute = minute < 10 ? ('0' + minute) : minute;
	return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
};

function sendJINGBANMsg(list, Wid, taskTitle, sender) {
	$.ajax({
		type: "POST",
		crossDomain: true,
		url: "../getinfo.ashx?types=send&userlist=",
		dataType: "json",
		data: {
			Ulist: list,
			context: "您经办的任务【" + taskTitle + "】有一条来自【" + sender + "】的新评论",
			pageUrl: "superviseDetail.html?TASKID=" + Wid + "&userid="
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert(JSON.stringify(XMLHttpRequest));
			alert(textStatus);
			alert(errorThrown);
		}
	});

}

function sendDUBANMsg(list, Wid, taskTitle, sender) {
	$.ajax({
		type: "POST",
		crossDomain: true,
		url: "../getinfo.ashx?types=send&userlist=",
		dataType: "json",
		data: {
			Ulist: list,
			context: "您督办的任务【" + taskTitle + "】有一条来自【" + sender + "】的新评论",
			pageUrl: "superviseDetail.html?TASKID=" + Wid + "&userid="
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert(JSON.stringify(XMLHttpRequest));
			alert(textStatus);
			alert(errorThrown);
		}
	});

}

function sendLeaderMsg(list, Wid, Uid, taskTitle, sender) {
	$.ajax({
		type: "POST",
		crossDomain: true,
		url: "../getinfo.ashx?types=send&userlist=",
		dataType: "json",
		data: {
			Ulist: list,
			context: "任务【" + taskTitle + "】有一条来自【" + sender + "】的新评论",
			pageUrl: "superviseDetail.html?TASKCODE=" + Wid + "&userid="
		},
		success: function() {
			location.href = "superviseDetail.html?TASKID=" + Wid + "&userid=" + Uid;
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert(JSON.stringify(XMLHttpRequest));
			alert(textStatus);
			alert(errorThrown);
		}
	});

}